<?php

namespace App\models\rbac;
use App\models\Base;
/**
* This is the model class for table "rbac_role_resource".
*
* @property integer $resource_id 
* @property integer $role_id 
*/
class RoleResource extends Base
{

    // 自动时间戳对应的变量
    //const CREATED_AT = 'created_at';
    //const UPDATED_AT = 'updated_at';

    protected $primaryKey = 'id';

    // 是否需要自动时间戳
    //public $timestamps = false;

    // 时间的格式类型
    //protected $dateFormat = 'Y-m-d';

    //protected $connection = 'default';

    protected $table = 'rbac_role_resource';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'resource_id','role_id'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [

    ];

    public static function SaveRoleRes($roleId , $resIds){
        $query = static::query();
        $query->where(['role_id' => $roleId])->delete() ;
        $all = [] ;
        foreach($resIds as  $resId){
            $all[] = ['role_id' => $roleId , 'resource_id' => $resId ] ;
        }
        return $query->insert($all);
    }

    public static function getRoleRes($roleId){
        return static::query()->where('role_id',$roleId)->get()->pluck('resource_id');
    }
}
